#pragma once

#include "localcommon.hpp"
#include <unordered_map>
#include <sstream>

namespace Local_Http
{
    const std::string Hspace = " ";
    const std::string Hblankline = "\r\n";
    const std::string Hdelimiitre = ": ";

    const std::string path = "./wwwroot";
    const std::string name = "easy.html";
    const std::string pathname = "./wwwroot/easy.html";

    /// @brief heep请求
    class Request
    {
    public:
        /**************Request***************** */
        Request() {}
        ~Request() {}
        /**************Member***************** */
        std::string Serialization()
        {
            std::string lo_ret;

            return lo_ret;
        }
        bool Deserialization(std::string message)
        {
            return false;
        }
        /**************Get***************** */
        /**************Set***************** */
        /**************Debug***************** */
        void print()
        {
        }

    private:
        std::string _method;
        std::string _url;
        std::string _version;

        std::unordered_map<std::string, std::string> _headers;
        std::string _blankline;
        std::string _text;
    };

    /// @brief http应答
    class Response
    {
    public:
        /**************Request***************** */
        Response() : _blankline(Hblankline) {}
        ~Response() {}
        /**************Member***************** */
        std::string Serialization()
        {
            std::string lo_ret = _version + Hspace + std::to_string(_code) + Hspace + _desc + Hblankline;
            for (auto &header : _headers)
            {
                lo_ret = header.first + Hdelimiitre + header.second + Hblankline;
            }
            lo_ret += _blankline;
            lo_ret += _text;
            return lo_ret;
        }
        bool Deserialization(std::string message)
        {
            return false;
        }
        /**************Get***************** */
        /**************Set***************** */
        /**************Debug***************** */
        void print()
        {
        }

    //private:
        std::string _version;
        int _code;
        std::string _desc;

        std::unordered_map<std::string, std::string> _headers;
        std::string _blankline;
        std::string _text;
    };

    /// @brief http任务
    class Http
    {
    public:
        /**************Request***************** */
        Http(uint16_t port) : _tcpser(std::make_unique<Local_TcpSer::TcpServer>(port)) {}
        ~Http() {}
        /**************MemberServer***************** */

        void GetRequest(std::shared_ptr<Local_Sock_Mod::Socket> &sockfd, Local_Inetaddr::Inetaddr &client)
        {
            // 获得报头

            // 解析报头，获得报文长度

            // 获得报文
            std::string lo_httpreqstr;
            int ret_rec = sockfd->Recv(&lo_httpreqstr);
            if (ret_rec > 0)
            {
                std::cout << "-----Request------" << std::endl;
                // 建立应答
                Response lo_resp;

                //反序列化

                //判断是不是找主页

                //查找目录


                //找的了 读对应文件
                std::string lo_filename = pathname;
                ReadFileContent(pathname, &(lo_resp._text));
                std::string lo_httpres = lo_resp.Serialization();
                std::cout << "-----Response------" << std::endl;
                sockfd->Send(lo_httpres);

                //未找到 返回错误码
            }
        }
        void SetResponse()
        {
        }
        void Start()
        {
            _tcpser->Start([this](std::shared_ptr<Local_Sock_Mod::Socket> &sockfd, Local_Inetaddr::Inetaddr &client)
                           { this->GetRequest(sockfd, client); });
        }
        /**************MemberClient***************** */
        /**************Get***************** */
        /**************Set***************** */
        /**************Debug***************** */
        void DebugGetRequest(std::shared_ptr<Local_Sock_Mod::Socket> &sockfd, Local_Inetaddr::Inetaddr &client)
        {
            std::string lo_httpreqstr;
            sockfd->Recv(&lo_httpreqstr);
            std::cout << "-----Request------" << std::endl;
            // 建立应答
            Response lo_resp;
            lo_resp._version = "HTTP/1.1";
            lo_resp._code = 200;
            lo_resp._desc = "OK";
            std::string lo_filename = pathname;
            ReadFileContent(pathname, &(lo_resp._text));
            std::string lo_httpres = lo_resp.Serialization();
            // std::string lo_ret_str = "<!DOCTYPE html>\
            //                             <html>\
            //                             <head>\
            //                             <title> 极简网页 | 2025 - 09 - 29 </title>\
            //                             <meta charset=\"UTF-8\">\
            //                             <style>body{font-family:sans-serif;margin:40px auto;max-width:600px;text-align:center;background:#f0f8ff;color:#333;line-height:1.6;}</style>\
            //                             </head>\
            //                             <body>\
            //                             <h1>HTTP/1.1极简示例</h1>\
            //                             <p>当前时间:<strong>2025年9月29日 星期一13:20</strong></p>\
            //                             <p>农历:<strong>乙巳蛇年 八月初八</strong></p>\
            //                             <hr>\
            //                             <p>这是一个符合HTTP/1.1协议的最简网页实现</p>\
            //                             <p>© 2025极简设计</p>\
            //                             </body>\
            //                             </html>";
            std::cout << "-----Response------" << std::endl;
            sockfd->Send(lo_httpres);
        }

    private:
        std::unique_ptr<Local_TcpSer::TcpServer> _tcpser;
    };
}